package com.yx.doodleview.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/**
 * Created by yx on 2017/9/22 0022.
 */

public class BitmapUtil {
    /**
     * 根据路径获取图片并且压缩，适应view
     *
     * @param filePath
     *            图片路径
     *            适应的view
     * @return Bitmap 压缩后的图片
     */
    public static Bitmap compressionFiller(String filePath)
    {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, opt);
        float scale = 0f;
        int bitmapHeight = bitmap.getHeight();
        int bitmapWidth = bitmap.getWidth();
        scale = bitmapHeight > bitmapWidth
                ? UIUtils.getScreenHeight() / (bitmapHeight * 1f)
                : UIUtils.getScreenWidth()  / (bitmapWidth * 1f);
        Bitmap resizeBmp;
        if (scale != 0)
        {
            int bitmapheight = bitmap.getHeight();
            int bitmapwidth = bitmap.getWidth();
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale); // 长和宽放大缩小的比例
            resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmapwidth,
                    bitmapheight, matrix, true);
        } else
        {
            resizeBmp = bitmap;
        }
        return resizeBmp;
    }
}
